package com.baizhi.controller;

import com.baizhi.entity.Hero;
import com.baizhi.service.HeroService;
import com.baizhi.service.impl.HeroServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/hero")
public class HeroController {
    private HeroService hs;

    public HeroService getHs() {
        return hs;
    }

    public void setHs(HeroService hs) {
        this.hs = hs;
    }

    @ResponseBody
    @RequestMapping("/selectByPage")
    public PageInfo<Hero> selectByPage(@RequestParam(defaultValue = "1") int pageNo){
        return hs.selectByPage(pageNo,5);
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(int id){
        Map map = new HashMap();
        try {
            hs.delete(id);
            map.put("state",0);
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",1);
            map.put("msg",e.getMessage());
        }
        return map;
    }
    @RequestMapping("/selectOne")
    @ResponseBody
    public Hero selectOne(int id){
        Hero hero = hs.selectOne(id);
        return hero;
    }
    @RequestMapping("/update")
    @ResponseBody
    public void update(Hero hero){
        hs.update(hero);
    }
    @RequestMapping("/add")
    @ResponseBody
    public void add(Hero hero){
        hs.add(hero);
    }
}
